package com.ruoyi.service.impl;

import com.ruoyi.DateGenerator;
import com.ruoyi.mapper.LendMessageMapper;
import com.ruoyi.mapper.PenaltyMapper;
import com.ruoyi.service.IStatisticsService;
import com.ruoyi.utils.StringSizeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class StatisticsServiceImpl implements IStatisticsService {
    @Resource
    private PenaltyMapper penaltyMapper;

    @Resource
    private LendMessageMapper lendMessageMapper;

    /**
     * 折线图：展示每一个月借出图书的数量
     *
     * @return
     */
    @Override
    public List lend(String year) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            //循环获取开始时间和结束时间   2024-03-20 14:26:15
            String s = i + "";
            String beginTime = year + "-" + StringSizeUtil.stringSize(s) + "-01 00:00:00";
            String endTime = year + "-" + StringSizeUtil.stringSize(s) + "-31 23:59:59";
            System.out.println(beginTime);
            System.out.println(endTime);
            //查询数据库
            Integer num = lendMessageMapper.getLendEachMonth(beginTime, endTime);
            //添加到list中
            list.add(num);
        }
        return list;
    }


    /**
     * 每一天借出图书数量变化
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public Map<String, Integer> getLendList(int year, int month) {
        HashMap<String, Integer> lendMap = new LinkedHashMap<>();
        //1.循环获取月份的天数   2024-03-18
        List<String> dates = DateGenerator.generateDates(year, month);
        for (String date : dates) {
            //2.查询数据库
            // 定义日期格式
            SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
            try {
                // 将字符串解析为 java.util.Date 对象
                Date day = dateFormatter.parse(date);
                int lendCount = lendMessageMapper.getLendEachDay(day);
                //3.添加到list中去
                lendMap.put(date, lendCount);
                //4.如果lendCount！=0就保存到count表中
                if (lendCount != 0) {
                    //4.1 count != 0 先去查询表中是否已经有了这条数据
                    //5.1先根据类型和日期查询数据库中是否已经存在该条数据
                    int count = lendMessageMapper.countByTypeAndCreateTime(1, date);
                    if (count == 1) {
                        //5.2存在，查询数量看看是不是和之前的一样
                        if (lendCount != lendMessageMapper.countNumber(1, date)) {
                            lendMessageMapper.updateNumber(1, date, lendCount);
                        }
                    }
                    //5.3不存在，直接插入
                    if (count != 1) {
                        lendMessageMapper.insertIntoTbaleCount(1, date, lendCount);
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return lendMap;
    }


    /**
     * 每个月图书丢失数量变化
     *
     * @param year
     * @return
     */
    @Override
    public List<Integer> getLostCount(String year) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            //1环获取开始时间和结束时间   2024-03-20 14:26:15
            String s = i + "";
            String beginTimeString = year + "-" + StringSizeUtil.stringSize(s) + "-01 00:00:00";
            String endTimeString = year + "-" + StringSizeUtil.stringSize(s) + "-31 23:59:59";
            //2.定义日期时间格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            //3.解析字符串为 LocalDateTime 对象
            LocalDateTime beginTime = LocalDateTime.parse(beginTimeString, formatter);
            LocalDateTime endTime = LocalDateTime.parse(endTimeString, formatter);
            //4.查询数据库
            Integer lostNumber = penaltyMapper.selectEveryMonthLostBook(beginTime, endTime);
            //添加到list中
            list.add(lostNumber);
            //5.保存到另一张表中进行持久化
            //5.1先根据类型和日期查询数据库中是否已经存在该条数据
            int count = lendMessageMapper.countByTypeAndCreateTime(2, beginTimeString);
            if (count == 1) {
                //5.2存在，查询数量看看是不是和之前的一样
                if (lostNumber != lendMessageMapper.countNumber(2, beginTimeString)) {
                    lendMessageMapper.updateNumber(2, beginTimeString, lostNumber);
                }
            }
            //5.3不存在，直接插入
            if (count != 1) {
                lendMessageMapper.insertIntoTbaleCount(2, beginTimeString, lostNumber);
            }
        }
        return list;
    }


    /**
     * 一年内每个月最流行的作者
     *
     * @return
     */
    @Override
    public Map<String, Integer> getMostPopularAuthor() {
        Map<String,Integer> mostPopularAuthorMap = new HashMap<>();
        //1.先查出所有的作者名字，作为key
        List<String> authorList  = lendMessageMapper.selectAllAuthor();
        System.out.println(authorList);
        for(String author : authorList){
            //2.根据名字查出对应的id
            int id = lendMessageMapper.selectAuthorId(author);
            Integer count = lendMessageMapper.selectCount(id);
            mostPopularAuthorMap.put(author,count);
        }
        //3.根据id查出次数,作为value保存到map中
        return mostPopularAuthorMap;
    }

    /**
     * 归还逾期率
     * @return
     */
    @Override
    public Map<Integer, Integer> backLaterStatistics() {
        Map<Integer, Integer> map = new HashMap<>();
        //1.查询总的数量
        Integer back = lendMessageMapper.selectAllNumber();
        //2.查询超时归还的数量
        Integer later = lendMessageMapper.selectBackLaterCount();
        //3.放到map集合中
        map.put(back-later,later);
        return map;
    }
}



